package com.tallerdealejandria.controladora.web;

import com.tallerdealejandria.util.Mapeador;
import com.tallerdealejandria.util.Mapeo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Oathmind
 */
public class ServletPrincipal extends HttpServlet {

    /**
     * Inicializa el mapeo al arrancar el servlet
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        super.init();
        generarMapeos(getServletContext());
    }

    /**
     * Inicia el mapeo y lo coloca en el servletContext
     * @param servletContext
     */
    private void generarMapeos(ServletContext servletContext) {
        Mapeador mapeador = new Mapeador();
        mapeador.generarMapeo();
        servletContext.setAttribute("mapeo", mapeador.getMapeos());
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-store");
        String url = request.getRequestURI();
        String accio = "";
        if (url.indexOf(".web") != -1) {
            accio = url.substring(url.lastIndexOf("/") + 1, url.indexOf(".web"));
        }
        HashMap<String, String> mapeo = new HashMap<String, String>();
        RequestDispatcher dispatch = request.getRequestDispatcher("index.jsp");
        ServletContext contexto = getServletContext();
        if (accio.equals("reload")) {
            generarMapeos(contexto);
        } else {
            ArrayList<Mapeo> mapeos = (ArrayList<Mapeo>) contexto.getAttribute("mapeo");
            Mapeo mapeoBusqueda = new Mapeo();
            mapeoBusqueda.setAccion(accio);
            int index = mapeos.indexOf(mapeoBusqueda);
            if (index != -1) {
                Mapeo mapeoFinal = mapeos.get(index);
                if (mapeoFinal.isUsarTemplate()) {
                    mapeo.put("titulo", mapeoFinal.getTitulo());
                    mapeo.put("href", mapeoFinal.getPagina());
                    request.setAttribute("mapeo", mapeo);
                    dispatch = request.getRequestDispatcher("template.jsp");
                } else {
                    dispatch = request.getRequestDispatcher(mapeoFinal.getPagina());
                }
            }
        }
        dispatch.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    // </editor-fold>
}
